#include "Nodes.h"

void searchNodes(IplImage* binaryImage, IplImage* skinImage,
				 IplImage* structureImage){
					 
	int height = binaryImage->height;
	int width  = binaryImage->width;
	int widthStep = skinImage->widthStep;
	uchar* skinPtr = (uchar*)skinImage->imageData;
	uchar* structurePtr = (uchar*)structureImage->imageData;
	memset(structurePtr, 0, sizeof(uchar)*height*widthStep);
	//memcpy(structurePtr, skinPtr, sizeof(uchar)*height*widthStep);

	for (int i = 1; i < height-1; i++){
		for (int j = 1; j < width-1; j++){
			if (*(binaryImage->imageData+i*width+j) == 120){
				structurePtr[i*widthStep+j*3] = 0;
				structurePtr[i*widthStep+j*3+1] = 0;
				structurePtr[i*widthStep+j*3+2] = 255;
				continue;
			}
			if (skinPtr[i*widthStep+j*3] == 255&&skinPtr[i*widthStep+j*3+2] == 0){
				structurePtr[i*widthStep+j*3] = 255;
				structurePtr[i*widthStep+j*3+1] = 0;
				structurePtr[i*widthStep+j*3+2] = 0;

				int cnt = 0;
				cnt = (skinPtr[(i-1)*widthStep+j*3] == 255 && skinPtr[(i-1)*widthStep+j*3+1] == 0) +
					  (skinPtr[(i+1)*widthStep+j*3] == 255 && skinPtr[(i+1)*widthStep+j*3+1] == 0) +
					  (skinPtr[i*widthStep+(j-1)*3] == 255 && skinPtr[i*widthStep+(j-1)*3+1] == 0) +
					  (skinPtr[i*widthStep+(j+1)*3] == 255 && skinPtr[i*widthStep+(j+1)*3+1] == 0);
				if ( cnt >=3 ){
					structurePtr[i*widthStep+j*3] = 0;
					structurePtr[i*widthStep+j*3+1] = 0;
					structurePtr[i*widthStep+j*3+2] = 255;
				}
			}
		}
	}
}